home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 3.iso / dist / fw_qt3.idb / usr / freeware / Qt / examples / demo / opengl / glcontrolwidget.cpp.z / glcontrolwidget.cpp
C/C++ Source or Header  |  2002-04-08  |  4KB  |  175 lines

  1. #include "glcontrolwidget.h"
  2.  
  3. #include <qcursor.h>
  4. #include <qtimer.h>
  5.  
  6. #include <math.h>
  7.  
  8. GLControlWidget::GLControlWidget( QWidget *parent, const char *name, QGLWidget *share, WFlags f )
  9. : QGLWidget( parent, name, share, f ),
  10.   xRot(0),yRot(0),zRot(0),xTrans(0),yTrans(0),zTrans(-10.0),scale(5.0), animation(TRUE), wasAnimated(FALSE), delay( 50 )
  11. {
  12.     setCursor( pointingHandCursor  );
  13.     timer = new QTimer( this );
  14.     connect( timer, SIGNAL(timeout()), SLOT(animate()) );
  15.     timer->start( delay );
  16. }
  17.  
  18. void GLControlWidget::transform()
  19. {
  20.     glTranslatef( xTrans, yTrans, zTrans );
  21.     glScalef( scale, scale, scale );
  22.  
  23.     glRotatef( xRot, 1.0, 0.0, 0.0 );
  24.     glRotatef( yRot, 0.0, 1.0, 0.0 );
  25.     glRotatef( zRot, 0.0, 0.0, 1.0 );
  26. }
  27.  
  28. /*!
  29.   Set the rotation angle of the object to \e degrees around the X axis.
  30. */
  31. void GLControlWidget::setXRotation( double degrees )
  32. {
  33.     xRot = (GLfloat)fmod(degrees, 360.0);
  34.     updateGL();
  35. }
  36.  
  37. /*!
  38.   Set the rotation angle of the object to \e degrees around the Y axis.
  39. */
  40. void GLControlWidget::setYRotation( double degrees )
  41. {
  42.     yRot = (GLfloat)fmod(degrees, 360.0);
  43.     updateGL();
  44. }
  45.  
  46.  
  47. /*!
  48.   Set the rotation angle of the object to \e degrees around the Z axis.
  49. */
  50. void GLControlWidget::setZRotation( double degrees )
  51. {
  52.     zRot = (GLfloat)fmod(degrees, 360.0);
  53.     updateGL();
  54. }
  55.  
  56. void GLControlWidget::setScale( double s )
  57. {
  58.     scale = s;
  59.     updateGL();
  60. }
  61.  
  62. void GLControlWidget::setXTrans( double x )
  63. {
  64.     xTrans = x;
  65.     updateGL();
  66. }
  67.  
  68. void GLControlWidget::setYTrans( double y )
  69. {
  70.     yTrans = y;
  71.     updateGL();
  72. }
  73.  
  74. void GLControlWidget::setZTrans( double z )
  75. {
  76.     zTrans = z;
  77.     updateGL();
  78. }
  79.  
  80. void GLControlWidget::setRotationImpulse( double x, double y, double z )
  81. {
  82.     setXRotation( xRot + 180*x );
  83.     setYRotation( yRot + 180*y );
  84.     setZRotation( zRot - 180*z );
  85. }
  86.  
  87. void GLControlWidget::setTranslationImpulse( double x, double y, double z )
  88. {
  89.     setXTrans( xTrans + 2*x );
  90.     setYTrans( yTrans - 2*y );
  91.     setZTrans( zTrans + 2*z );
  92. }
  93.  
  94. void GLControlWidget::mousePressEvent( QMouseEvent *e )
  95. {
  96.     e->accept();
  97.     oldPos = e->pos();
  98. }
  99.  
  100. void GLControlWidget::mouseReleaseEvent( QMouseEvent *e )
  101. {
  102.     e->accept();
  103.     oldPos = e->pos();
  104. }
  105.  
  106. void GLControlWidget::mouseMoveEvent( QMouseEvent *e )
  107. {
  108.     e->accept();
  109.     double dx = e->x() - oldPos.x();
  110.     double dy = e->y() - oldPos.y();
  111.  
  112.     oldPos = e->pos();
  113.  
  114.     double rx = dx / width();
  115.     double ry = dy / height();
  116.  
  117.     if ( e->state() == LeftButton )
  118.     setRotationImpulse( ry, rx, 0 );
  119.     else if ( e->state() == RightButton )
  120.     setRotationImpulse( ry, 0, rx );
  121.     else if ( e->state() == MidButton )
  122.     setTranslationImpulse( rx, ry, 0 );
  123.     else if ( e->state() == ( LeftButton | RightButton ) )
  124.     setTranslationImpulse( rx, 0, ry );
  125. }
  126.  
  127. void GLControlWidget::wheelEvent( QWheelEvent *e )
  128. {
  129.     e->accept();
  130.     if ( scale <= ( (double)e->delta() / 1000 ) )
  131.     return;
  132.     setScale( scale - ( (double)e->delta() / 1000 ));
  133. }
  134.  
  135. void GLControlWidget::mouseDoubleClickEvent( QMouseEvent * )
  136. {
  137.     if ( delay <= 0 )
  138.     return;
  139.  
  140.     animation = !animation;
  141.     if ( animation )
  142.     timer->start( delay );
  143.     else
  144.     timer->stop();
  145. }
  146.  
  147. void GLControlWidget::showEvent( QShowEvent *e )
  148. {
  149.     if ( wasAnimated && !timer->isActive() )
  150.     timer->start( delay );
  151.  
  152.     QGLWidget::showEvent( e );
  153. }
  154.  
  155. void GLControlWidget::hideEvent( QHideEvent *e )
  156. {
  157.     wasAnimated = timer->isActive();
  158.     timer->stop();
  159.     QGLWidget::hideEvent( e );
  160. }
  161.  
  162. void GLControlWidget::animate()
  163. {
  164. }
  165.  
  166. void GLControlWidget::setAnimationDelay( int ms )
  167. {
  168.     timer->stop();
  169.     delay = ms;
  170.     if ( animation ) {
  171.     wasAnimated = TRUE;
  172.     timer->start( delay );
  173.     }
  174. }
  175.